package com.fqgj.xjd.user.service.impl;
/**
 * Created by zhangxingyun on 17/7/21.
 */

import com.fqgj.common.api.Response;
import com.fqgj.xjd.user.client.UserAuthKeyPaymentService;
import com.fqgj.xjd.user.dao.UserAuthPaymentDao;
import com.fqgj.xjd.user.client.response.UserAuthPayment;
import com.fqgj.xjd.user.entity.UserAuthPaymentEntity;
import com.fqgj.xjd.user.client.enums.UserBindCardTypeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:
 * @Description: 认证支付关系维护服务
 * @Author zhangxingyun
 * @Create 2017-07-21 09:37
 */
@Service("userAuthKeyPaymentService")
public class UserAuthKeyPaymentServiceImpl implements UserAuthKeyPaymentService {


    @Autowired
    UserAuthPaymentDao userAuthPaymentDao;

    /**
     * 51、连连认证关系维护
     * @param userAuthPayment
     * @param userBindCardTypeEnum
     * @return
     */
    @Override
    @Transactional
    public Response<Boolean> addUserAuthKey(UserAuthPayment userAuthPayment, UserBindCardTypeEnum userBindCardTypeEnum) {
        UserAuthPaymentEntity oldEntity = userAuthPaymentDao.selectUserAuthKeyByUserCodeAndType(userAuthPayment.getUserCode(), userBindCardTypeEnum.getCode());
        //不重复绑定
        if (oldEntity != null) {
            userAuthPaymentDao.logicDelete(oldEntity.getId());
        }
        UserAuthPaymentEntity userAuthPaymentEntity = new UserAuthPaymentEntity();
        BeanUtils.copyProperties(userAuthPayment,userAuthPaymentEntity);
        userAuthPaymentEntity.setType(userBindCardTypeEnum.getCode());
        userAuthPaymentDao.insert(userAuthPaymentEntity);
        return Response.ok();
    }

    /**
     * 获取认证关系
     * @param userCode
     * @param userBindCardTypeEnum
     * @return
     */
    @Override
    public Response<UserAuthPayment> getUserAuthKey(String userCode, UserBindCardTypeEnum userBindCardTypeEnum) {
        UserAuthPaymentEntity userAuthPaymentEntity = userAuthPaymentDao.selectUserAuthKeyByUserCodeAndType(userCode,userBindCardTypeEnum.getCode());

        if(null!=userAuthPaymentEntity){
            UserAuthPayment userAuthPayment = new UserAuthPayment(userAuthPaymentEntity.getUserCode(),userAuthPaymentEntity.getUserKey(), userAuthPaymentEntity.getThirdPartyUserKey());
            return Response.ok().putData(userAuthPayment);
        }

        return Response.ok();
    }

    /**
     * 获取认证关系
     * @param userCodes
     * @param userBindCardTypeEnum
     * @return
     */
    @Override
    public Response<List<UserAuthPayment>> getUserAuthKeyByUserCodes(List<String> userCodes, UserBindCardTypeEnum userBindCardTypeEnum) {
        List<UserAuthPaymentEntity> userAuthPaymentEntityList =userAuthPaymentDao.selectUserAuthKeyByUserCodesAndType(userCodes,userBindCardTypeEnum.getCode());

        if(userAuthPaymentEntityList.size()>0){
            List<UserAuthPayment> userAuthPaymentList = new ArrayList<>();
            for(UserAuthPaymentEntity userAuthPaymentEntity:userAuthPaymentEntityList){
                UserAuthPayment userAuthPayment = new UserAuthPayment(userAuthPaymentEntity.getUserCode(),userAuthPaymentEntity.getUserKey(), userAuthPaymentEntity.getThirdPartyUserKey());
                userAuthPaymentList.add(userAuthPayment);
            }
            return Response.ok().putData(userAuthPaymentList);
        }
        return Response.ok();
    }
}
